package com.baosight.sczx.zm.ce.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.ce.Equipment;
import com.baosight.sczx.common.zm.cInterface.ce.RerollingStatus;
import com.baosight.sczx.common.zm.cInterface.ce.WorkPlan;
import com.baosight.sczx.zm.ce.domain.ZMCEEquipmentOrder;

/**
 * <p>Title: ServiceZMCE10.java</p>
 * <p>Description: 工单管理</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-13 下午05:31:42  张小飞  Create
 *</pre>
 */
public class ServiceZMCE10 extends ServiceEPBase {
	
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19上午10:42:36 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19上午10:42:36 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdFounderDepId", UserSession.getInSessionProperty("orgCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCEEquipmentOrder.query", new ZMCEEquipmentOrder());
		
		outInfo.addBlock(Equipment.getEquipmentBlock("cmes.ce.equipment", null));
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.department", "orgCode", "orgName", false));		
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.ce.people", "empCode", "empName", false));
		//outInfo.addBlock(WorkPlan.getWorkPlanBlock("cmes.ce.workPlan", null));
		
		return outInfo;
	}

	/**
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19上午10:42:36 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验编号是否重复
		boolean validateValue = validate(inInfo, "ZMCEEquipmentOrder.validateForMaintenanceApply", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		String fdSource = inInfo.getBlock("result").getCellStr(0, "fdSource");
		String fdSourceId = inInfo.getBlock("result").getCellStr(0, "fdPlanId");
		String workrecordId = inInfo.getBlock("result").getCellStr(0, "workrecordId");
		
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_EQUIPMENTORDER"));
		inInfo.getBlock("result").setCell(0, "fdFounderId", UserSession.getUserId());
		inInfo.getBlock("result").setCell(0, "fdFounder", UserSession.getInSessionProperty("UserCname"));
		inInfo.getBlock("result").setCell(0, "fdFounderDepId", UserSession.getInSessionProperty("orgCode"));


		String hasPlan = inInfo.getBlock("result").getCellStr(0, "hasPlan");
		if("yes".equals(hasPlan)){
			super.update(inInfo, "ZMCEWorkPlan.updateNote");
		}
			
		if("执行".equals(inInfo.getBlock("result").getCell(0, "fdStatus")))
		{
			inInfo.getBlock("result").setCell(0, "fdTrueBeganTime", StaticTools.getDateTime());
		
			if("大修".equals(inInfo.getBlock("result").getCell(0, "fdType"))||"维修".equals(inInfo.getBlock("result").getCell(0, "fdType")))
			{
				if(Equipment.updateEquipmentStatus("维修", "不完好", inInfo.getBlock("result").getCellStr(0, "fdEquipmentId"))<=0)
				{
					inInfo.setStatus(-1);
					return inInfo;
				}
			}
		}
		EiInfo outInfo = super.insert(inInfo, "ZMCEEquipmentOrder.insert");
		
		//异常问题模块中生成计划中调用
		if("异常".equals(fdSource)){
			inInfo.getBlock("result").setCell(0, "fdKeyId", fdSourceId);
			inInfo.getBlock("result").setCell(0, "fdStatus", "处理中");
			inInfo.getBlock("result").setCell(0, "fdSolveDepartment", UserSession.getInSessionProperty("orgName"));
			inInfo.getBlock("result").setCell(0, "fdSolvePerson", UserSession.getInSessionProperty("UserCname"));
			inInfo.getBlock("result").setCell(0, "fdProcessDatetime", StaticTools.getDateTime());
			super.update(inInfo, "ZMCEUnusualProblem.updateAfterGenerate");
			
			inInfo.getBlock("result").setCell(0, "fdKeyId", workrecordId);
			inInfo.getBlock("result").setCell(0, "fdStatus", "处理中");
			super.update(inInfo, "ZMCEFaultRecord.updateStatus");
		}
		
		return outInfo;
		
		
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19上午10:42:36 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验编号是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		boolean validateValue = validate(inInfo, "ZMCEEquipmentOrder.validateForMaintenanceApply", params);
		
		if(validateValue) {
			return inInfo;
		}		
		inInfo.getBlock("result").setCell(0, "fdModifier", UserSession.getInSessionProperty("UserCname"));
		EiInfo outInfo = super.update(inInfo, "ZMCEEquipmentOrder.update");
		return outInfo;
	}
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19上午10:42:36 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCEEquipmentOrder.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: effect</p>
	 * <p>Description: 生效</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-21上午10:34:53 张小飞
	 * </p>
	 *
	 */
	public EiInfo effect(EiInfo inInfo) {
		for(int i=0; i<inInfo.getBlock("result").getRowCount();i++)
		{
			if("大修".equals(inInfo.getBlock("result").getCell(i, "fdType"))||"维修".equals(inInfo.getBlock("result").getCell(i, "fdType")))
			{
				if(Equipment.updateEquipmentStatus("维修", "不完好", inInfo.getBlock("result").getCellStr(i, "fdEquipmentId"))<=0)
				{
					inInfo.setStatus(-1);
					return inInfo;
				}
			}
			inInfo.getBlock("result").setCell(i, "fdTrueBeganTime", StaticTools.getDateTime());
		}
		EiInfo _temp = super.update(inInfo, "ZMCEEquipmentOrder.effect");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: start</p>
	 * <p>Description: 启用</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-21上午10:35:07 张小飞
	 * </p>
	 *
	 */
	public EiInfo start(EiInfo inInfo) {
		for(int i=0; i<inInfo.getBlock("result").getRowCount();i++)
		{
			inInfo.getBlock("result").setCell(i, "fdTrueBeganTime", StaticTools.getDateTime());
		}
		EiInfo _temp = super.update(inInfo, "ZMCEEquipmentOrder.start");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
//	/**
//	 * 
//	 * <p>Function: stop</p>
//	 * <p>Description: 停止</p>
//	 * <p>return value:EiInfo</p>
//	 * <p>throws Exception:</p>
//	 * <p>History:
//	 *    2011-12-21上午10:35:19 张小飞
//	 * </p>
//	 *
//	 */
//	public EiInfo stop(EiInfo inInfo) {
//		EiInfo _temp = super.update(inInfo, "ZMCEEquipmentOrder.stop");
//		EiInfo outInfo = this.query(inInfo);
//        outInfo.setMsg(_temp.getMsg());
//        outInfo.setDetailMsg(_temp.getDetailMsg());
//		return outInfo;
//	}
	
	/**
	 * 
	 * <p>Function: invalid</p>
	 * <p>Description: 作废</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-21上午10:35:32 张小飞
	 * </p>
	 *
	 */
	public EiInfo invalid(EiInfo inInfo) {
		RerollingStatus.rerolling(inInfo, "result", 1);
		EiInfo _temp = super.update(inInfo, "ZMCEEquipmentOrder.invalid");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 校验重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:26:16 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {		
		if(params == null) {
			params = new HashMap<String, String>();
		}
		params.put("fdCode", inInfo.getBlock("result").getCellStr(0, "fdCode"));
		if("".equals(inInfo.getBlock("result").getCellStr(0, "fdCode")))//编号为空时不需要校验
		{
			return false;
		}
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("编号不能重复！");
			return true;
		}
		return false;
	}
}
